package com.sbl.bajw.pc.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.fileupload.FileItem;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.sbl.bajw.common.MyBatisDao;
import com.sbl.bajw.common.web.MessageException;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.mapper.PersonnelRecordDao;
import com.sbl.bajw.pc.model.AttFileModel;
import com.sbl.bajw.pc.model.PersonnelRecordModel;
import com.sbl.bajw.util.ExecptionUtil;
import com.sbl.bajw.util.LogUtil;

@Service
public class PersonnelRecordService {

	@Resource
	private PersonnelRecordDao dao;
	
	@Resource
	private AttFileService attfileService;
	
	@Resource
	private MyBatisDao gridDao;
	
	static Logger log;
	
	static {
		log = LogUtil.getLogger("PersonnelRecord");
	}
	
	public void insert(PersonnelRecordModel model) throws Exception{
		log.info(model.getCreater() + "开始入库，属性：" + model.toString());
		try {
			if(model.getLon() < 100){
				log.info(model.getCreater() + "经纬度不合法");
				throw new MessageException("经纬度不合法", "3");
			}
			if(model.getUuid() != null && dao.getUuidNum(model.getUuid()) > 0){
				log.info(model.getCreater() + "重复提交");
				throw new MessageException("重复提交", "3");
			}
			uploadFile(model);
			dao.insert(model);
			log.info(model.getCreater() + "入库成功");
		} catch (Exception e) {
			String msg = ExecptionUtil.getStackTrace(e);
			log.info("录入异常："+msg);
			throw e;
		}
	}
	
	public void update(PersonnelRecordModel model) throws Exception{
		dao.update(model);
	}
	
	public PersonnelRecordModel getModel(String id){
		PersonnelRecordModel model = dao.getModel(id);
		return model;
	}
	
	public String getList(HashMap<String, Object> parm){
		String result = gridDao.selectPage(PersonnelRecordDao.class, parm, "getList");
		return result;
	}
	
	public List<PersonnelRecordModel> getListApp(HashMap<String, String> parm){
		List<PersonnelRecordModel> list = dao.getListApp(parm);
		return list;
	}
	
	public int getCountApp(HashMap<String, String> parm){
		int num = dao.getCountApp(parm);
		return num;
	}
	
	public PersonnelRecordModel getModelApp(String personId){
		PersonnelRecordModel model = dao.getModelApp(personId);
		return model;
	}
	
	public void uploadFile(PersonnelRecordModel model) throws Exception {
		String id = dao.getPersonId();

		if (id == null || id.equals("")) {
			throw new MessageException("ID不能为空", "2");
		}

		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmssS");
		
		AttFileModel attfile = null;
		for(int i = 1; i < 5; i++){
			FileItem item = RequestUnit.getFileItem("img" + i);
			if(item != null){
				String dataStr = sf.format(Calendar.getInstance().getTime());
				AttFileModel filemodel = new AttFileModel();
				filemodel.setBusinessType("9");
				filemodel.setFileDir("personnel_record/image");
				filemodel.setFileName(dataStr + i +  "_img_" + id);
				filemodel.setGroupid(id);
				filemodel.setItem(item);
				filemodel.setUserid(model.getCreater());
				filemodel.setFiletype("img");
				attfile = attfileService.insert(filemodel);
				log.info(model.getCreater() + "图片上传成功");
				switch (i) {
				case 1: model.setFile1(attfile.getSmallimgurl()); break;
				case 2: model.setFile2(attfile.getSmallimgurl()); break;
				case 3: model.setFile3(attfile.getSmallimgurl()); break;
				case 4: model.setFile4(attfile.getSmallimgurl()); break;
				}
			}
		}
		
		model.setPersonId(id);
	}
}
